package com.xhwl.logistics.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.sdata.Project;
import com.xhwl.common.query.cent.sdata.AccountQuery;
import com.xhwl.common.query.cent.sdata.ProjectQuery;
import com.xhwl.common.query.cent.vehicle.AreaQuery;
import com.xhwl.common.vo.cent.vehicle.AreaPageVO;
import com.xhwl.logistics.dao.VehcAreaDao;
import com.xhwl.common.pojo.cent.vehicle.VehcArea;
import com.xhwl.logistics.service.IVehcAreaService;
import com.xhwl.logistics.service.cent.IAccountService;
import com.xhwl.logistics.service.cent.IProjectService;
import com.xhwl.logistics.util.EscapeCharacter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lu.Aaron
 * @since 2021-02-08
 */
//@Service
public class VehcAreaServiceImpl extends ServiceImpl<VehcAreaDao, VehcArea> implements IVehcAreaService {

    @Autowired
    private VehcAreaDao vehcAreaDao;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IProjectService iProjectService;
    @Override
    public List<VehcArea> list(AreaQuery areaQuery) {
        QueryWrapper<VehcArea> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(areaQuery.getParkingLotId())) {
            wrapper.eq("parking_lot_id", areaQuery.getParkingLotId());
        }
        if (Objects.nonNull(areaQuery.getOrgId())) {
            wrapper.eq("org_id", areaQuery.getOrgId());
        }
        if (Objects.nonNull(areaQuery.getId())) {
            wrapper.eq("id", areaQuery.getId());
        }
        return vehcAreaDao.selectList(wrapper);
    }

    @Override
    public ResultJson areaPage(AreaQuery areaQuery) {
        /**
         * 对模糊查询字段停车场名称字段进行判断操作，防止sql注入
         */
        areaQuery.setParkingLotName(EscapeCharacter.escape(areaQuery.getParkingLotName()));
        Integer current = areaQuery.getCurrent();
        Integer pageSize = areaQuery.getPageSize();
        areaQuery.setCurrent((areaQuery.getCurrent() - 1) * areaQuery.getPageSize());
        List<AreaPageVO> areaPageVOS = vehcAreaDao.queryAreaList(areaQuery);
        List<Integer> projectIds = areaPageVOS.stream().map(v -> v.getProjectId()).collect(Collectors.toList());
        ProjectQuery projectQuery = new ProjectQuery();
        projectQuery.withIdList(projectIds);
        List<Project> projects = iProjectService.list(projectQuery, null);
        List<Integer> accountIds = areaPageVOS.stream().map(v -> v.getCreatorAccountId()).collect(Collectors.toList());
        AccountQuery accountQuery = new AccountQuery();
        accountQuery.setIdList(accountIds);
        List<Account> accounts = accountService.getAccountList(null, accountQuery);
        areaPageVOS.forEach(apv->{
            projects.forEach(p->{
                if (apv.getProjectId().equals(p.getId())) {
                    apv.setProjectName(p.getName());
                }
            });
            if (Objects.nonNull(apv.getCreatorAccountId())) {
                accounts.forEach(a->{
                    if (apv.getCreatorAccountId().equals(a.getId())) {
                        apv.setCreatorAccountName(a.getName());
                    }
                });
            }
        });
        Long total = vehcAreaDao.queryAreaTotal(areaQuery);
        Page<AreaPageVO> page = new Page<>();
        page.setTotal(total);
        page.setCurrent(current);
        page.setSize(pageSize);
        page.setRecords(areaPageVOS);
        return ResultJson.success(page);
    }
}
